﻿
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace XChipTool
{
  public  class WaitBefore
    {
        private static DevExpress.Utils.WaitDialogForm WaitBeforeLogin = null;
        private static CancellationTokenSource cts = new CancellationTokenSource();
        /// <summary>
        /// 开启
        /// </summary>
        /// <param name="name"></param>
        /// <param name="msg"></param>
        public static void Start(string name, string msg) {
            if (cts != null)
            {
               
                cts.Cancel();
                cts.Dispose();
            }
            if (WaitBeforeLogin != null)
            {
                Close();
            }

            cts = new CancellationTokenSource();
            Task.Run(() =>
            {
                WaitBeforeLogin = new DevExpress.Utils.WaitDialogForm(name, msg);
                WaitBeforeLogin.StartPosition = FormStartPosition.CenterParent;
                Application.Run(WaitBeforeLogin);
            }, cts.Token);
        }

        /// <summary>
        /// 开启
        /// </summary>
        /// <param name="name"></param>
        /// <param name="msg"></param>
        public static void Start(string name, string msg,int timer)
        {
            if (cts != null)
            {
               
                cts.Cancel();
                cts.Dispose();
            }
            if (WaitBeforeLogin != null)
            {
                Close();
            }
            cts = new CancellationTokenSource();
            Task.Run(() =>
            {
                WaitBeforeLogin = new DevExpress.Utils.WaitDialogForm(name, msg);
                WaitBeforeLogin.StartPosition = FormStartPosition.CenterParent;
                Application.Run(WaitBeforeLogin);
                Thread.Sleep(timer * 1000);
                Close();
            }, cts.Token);
        }
        /// <summary>
        /// 关闭
        /// </summary>
        public static void Close() {
            try
            {
               
                WaitBeforeLogin.Invoke((EventHandler)delegate { WaitBeforeLogin.Close(); });
            }
            catch 
            {
                //cts.Cancel();
                return;
            }
        }

        public static class TopMostMessageBox
        {
            public static DialogResult Show(string text, string caption, MessageBoxButtons buttons, MessageBoxIcon icon)
            {
                Form topMostForm = new Form();
                topMostForm.TopMost = true;
                topMostForm.FormBorderStyle = FormBorderStyle.FixedDialog;
                topMostForm.StartPosition = FormStartPosition.CenterScreen;

                DialogResult result = MessageBox.Show(topMostForm, text, caption, buttons, icon);

                topMostForm.Dispose();

                return result;
            }
        }
    }
}
